package JGameAdapter;


import java.util.Set;
import jgame.JGColor;
import jgame.JGPoint;
import jgame.platform.JGEngine;

/**
 *
 * @author Marvin
 */
class Engine extends JGEngine {

    private Set<ParticleInterface> particles;
    private JGColor color;
    private Pedal bremspedal;
    private Pedal gaspedal;
    private ParticleInterface particle;
    private static boolean absIsSet;
    private static boolean asrIsSet;
    private int ground;
    private int ground_old = -1;

    
    public Engine(JGPoint size) {
        // This inits the engine as an application.
        initEngine(size.x, size.y);
    }
       
    public void setParticles(Set particles) {
        this.particles = particles;
    }

	public void initCanvas() {
		// The only thing we need to do in this method is to tell the engine
		// what canvas settings to use.  We should not yet call any of the
		// other game engine methods here!
		setCanvasSettings(
			20,  // width of the canvas in tiles
			15,  // height of the canvas in tiles
			16,  // width of one tile
			16,  // height of one tile
			     //    (note: total size = 20*16=320  x  15*16=240)
			null,// foreground colour -> use default colour white
			JGColor.green,// background colour -> use default colour black
			null// standard font -> use default font
		);
	}

	/** This method is called when the engine has finished initialising and is
	 * ready to produce frames.  Note that the engine logic runs in its own
	 * thread, separate from the AWT event thread, which is used for painting
	 * only.  This method is the first to be called from within its thread.
	 * During this method, the game window shows the intro screen. */
	public void initGame()  {
		// We can set the frame rate, load graphics, etc, at this point. 
		// (note that we can also do any of these later on if we wish)
            defineMedia("media.tbl");
		setFrameRate(
			50,// 50 = frame rate, 50 frames per second
			2  //  2 = frame skip, skip at most 2 frames before displaying
			   //      a frame again
                        );

                         // Simulation der Pedale
                         bremspedal = new Pedal(1.0, 0.1, 0.0);
                         gaspedal = new Pedal(1.0, 0.01, 0.0);
                             
                        
                          // Objekt holen
                          for(Object value: particles){
                            particle = (ParticleInterface) value;
                          }
                       
                         // alles auf Default setzen
                         particle.reset();
                         
                         // Hintergrund ist invalid
                         ground = 0;
        }
        

	/** Game logic is done here.  No painting can be done here, define
	* paintFrame to do that. */
        @Override
    public void doFrame() {
        /**************************************************
         ***************Gas und Bremse*********************
         **************************************************/

        bremspedal.step( getKey(KeyDown) );
        gaspedal.step( getKey(KeyUp) );
              
         /**************************************************
         ***************Traktionssteuerung*********************
         **************************************************/
        if (getKey('A'))
        {
            particle.setTraction(1.0);
            ground = 0;
        }
        if (getKey('S')) 
        {
            particle.setTraction(0.7);
            ground = 1;
        }
        if (getKey('D')) 
        {
            particle.setTraction(0.3);
            ground = 2;
        }
        if (getKey('F'))
        {
            particle.setTraction(0.1);
            ground = 3;
        } 
        if (getKey('R'))
        {
            particle.reset();
        }
        if (getKey(','))
        {
            if(!absIsSet)
            {
                if(particle.getAbs())
                    particle.setAbs(false);
                else
                    particle.setAbs(true);
            
                absIsSet = true;
            }
        }
        else
        {
            absIsSet = false;
        }
        
        if (getKey('.'))
        {
            if(!asrIsSet)
            {
                if(particle.getAsr())
                    particle.setAsr(false);
                else
                    particle.setAsr(true);
                
                asrIsSet = true;
            }
        }
        else
        {
            asrIsSet = false;
        }

        particle.step( 0.02, gaspedal.value(), bremspedal.value() );
    }
   
	/** Any graphics drawing beside objects or tiles should be done here.
	 * Usually, only status / HUD information needs to be drawn here. */
	public void paintFrame()
        { 
        //Autoposition berechnen
        int HALF_WIDTH = pfHeight() / 2;
        double AUTO_POS = particle.getPos()*4.0%pfWidth();
                // Hintergrund
                if(ground != ground_old)
                {
                    switch(ground)
                    {
                        case 0:
                            setBGImage("strasse");
                            break;
                        case 1:
                            setBGImage("naesse");
                            break;
                        case 2:
                            setBGImage("schnee");
                            break;
                        case 3:
                            setBGImage("eis");
                            break;
                        default:
                            setBGImage("strasse");
                    }
                    // merken
                    ground_old = ground;
                }
                        
                
                setColor(JGColor.black);
                if(particle.abflug())
                {
                    particle.set(particle.getTime(), particle.getPos(), 0.0, 0.0, 0.0);
                    int bild =  (((int)(particle.getTime()*1000)) % 200);
                    if (bild < 50) {
                        drawImage(particle.getPos() * 4.0 % pfWidth(), HALF_WIDTH, "driving_particle");
                    } else if (bild < 100) {
                        drawImage(particle.getPos() * 4.0 % pfWidth(), HALF_WIDTH, "driving_particle2");
                    } else if (bild < 150) {
                        drawImage(particle.getPos() * 4.0 % pfWidth(), HALF_WIDTH, "driving_particle3");
                    } else if (bild < 200) {
                        drawImage(particle.getPos() * 4.0 % pfWidth(), HALF_WIDTH, "driving_particle4");
                    }
                        
                }
                else
                {
                        drawImage(AUTO_POS, pfHeight()/2, "driving_particle");
                }
                
		drawString("speed......." + Math.round(particle.getSpeedInKmH()) + "km/h", 200, 10, -1);
                drawString("time........" + (int)particle.getTime() + "s", 200, 20, -1);
                drawString("Pos........." + (int)particle.getPos() + "m", 200, 30, -1);
                drawString("Level......." + particle.getLevel(), 200, 40, -1);
                drawString("Brake......." + particle.getBrake(), 200, 50, -1);
                drawString("ABS........." + particle.getAbs(), 200, 60, -1);
                drawString("ASR........." + particle.getAsr(), 200, 70, -1);
    
                // Regelt?
                setColor(JGColor.red);
                if(particle.absActive()) drawString("ABS regelt", 200, 80, -1);
                if(particle.asrActive()) drawString("ASR regelt", 200, 90, -1);
                if(particle.abflug())    drawString("Abflug", 200, 100, -1);
           
	}

    /** Any graphics drawing beside objects or tiles should be done here.
     * Usually, only status / HUD information needs to be drawn here. */

    }


    

